package utils

import (
	"math"
	"net"
	"strconv"
	"strings"
	"time"
)
//服务端ip
func GetLocalIP() (ip string, err error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return
	}
	for _, addr := range addrs {
		ipAddr, ok := addr.(*net.IPNet)
		if !ok {
			continue
		}
		if ipAddr.IP.IsLoopback() {
			continue
		}
		if !ipAddr.IP.IsGlobalUnicast() {
			continue
		}
		return ipAddr.IP.String(), nil
	}
	return
}

//获取相差时间
func GetHourDiffer(startTime, endTime string) int64 {
	var hour int64
	t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
	t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
	if err == nil && t1.Before(t2) {
		diff := t2.Unix() - t1.Unix() //
		hour = diff / 3600
		return hour
	} else {
		return hour
	}
}

//对”https://111.com , http://222.com“ 转换成数组
func StringToStrings (str string) []string{
	strsnew:=make([]string,0)
	if len(str)>0 {
		strs:=strings.Split(str,",")
		strsnew=strs
	}


	return strsnew
}
//对数组 转换成 ”https://111.com , http://222.com“
func StringsToString (strs []string) string{
	str:=""
	for _,v:=range strs{
		if str!="" {
			str += ","
		}
		str += v
	}

	return str
}


//对”https://111.com , http://222.com“ 转换成int数组
func IntToInts (str string) []int{
	strsnew:=make([]int,0)
	if len(str)>0 {
		strs:=strings.Split(str,",")
		for _,v:=range strs{
			number, _ := strconv.Atoi(v)
			strsnew= append(strsnew,number )
		}
	}
	return strsnew
}
//对数组 转换成 ”https://111.com , http://222.com“
func IntsToInt (strs []int) string{
	str:=""
	for _,v:=range strs{
		if str!="" {
			str += ","
		}
		string := strconv.Itoa(v)
		str += string
	}
	str=strings.Replace(str,"[","",-1)
	str=strings.Replace(str,"]","",-1)
	return str
}

//string 转 [][]float64
func StringToFloat64ContainFloat64(str string) (float [][]float64){
	//去掉头【【 和尾部 】】
	str=str[2:len(str)-2]
	var  strnew []string
	if len(str)>0 {
		strnew=strings.Split(str,"],[")
	}

	for _,v:=range strnew{
		var floatson []float64
		one:=strings.Split(v,",")
		for _,o:=range one{
			float,_ := strconv.ParseFloat(o,64)
			floatson= append(floatson, float)
		}

		float= append(float, floatson)
	}

return

}

//string 转 []float64
func StringToFloat64(str string) (float [][]float64){
	//去掉头【【 和尾部 】】
	str=str[2:len(str)-2]
	var  strnew []string
	if len(str)>0 {
		strnew=strings.Split(str,"],[")
	}

	for _,v:=range strnew{
		var floatson []float64
		one:=strings.Split(v,",")
		for _,o:=range one{
			float,_ := strconv.ParseFloat(o,64)
			floatson= append(floatson,float)
		}

		float= append(float, floatson)

	}

	return

}

//取小数点
func FormatFloat(num float64, decimal int) string {
	// 默认乘1
	d := float64(1)
	if decimal > 0 {
		// 10的N次方
		d = math.Pow10(decimal)
	}
	// math.trunc作用就是返回浮点数的整数部分
	// 再除回去，小数点后无效的0也就不存在了
	return strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
}

//取2位小数点 四舍五入
func FormatFloat64(num float64, decimal int) float64 {
	n10 := math.Pow10(decimal)
	num2 := math.Trunc((num+0.5/n10)*n10) / n10
	return num2
}

//取2位小数点 四舍五入 负数
func FormatFloat641(num float64, decimal int) float64 {
	n10 := math.Pow10(decimal)
	num2 := math.Trunc((num-0.5/n10)*n10) / n10
	return num2
}

//根据身份证获取年龄
func IdCardAge(idcard string) (age int){

	if len(idcard)>10 {
		year:=idcard[6:10]
		nowyear:=times.Now().Year()
		yearstr, _ := strconv.Atoi(year)
		age=nowyear-yearstr
	}
	if	len(idcard)!=18{
		age=0
	}

	return age
}

//输入俩时间得出天数，小时  t1:最早的时间  t2:最迟的时间  state 1 :day  2:hour
func CompeleteTime(t1 int64 ,t2 int64,state int) float64{
	t3:=t2-t1

	var res float64

	//天
	if state==1 {
		res=FormatFloat64(float64(t3)/60/60/24,2)
	}
	//小时
	if state==2 {
		res=FormatFloat64(float64(t3)/60/60,2)
	}

	return res
}

//判断int64 数组中是否存在某值
func ContainInt(input int,text []int) bool{
	for _,v:=range text{
		if input==v {
			return true
		}
	}
	return false
}

